<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Parse callbacks</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="yaml.callbacks.html">« Callbacks</a></li>
      <li style="float: right;"><a href="yaml.callbacks.emit.html">Emit callbacks »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="yaml.callbacks.html">Callbacks</a></li>
    <li>Parse callbacks</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="yaml.callbacks.parse" class="section">
  <h2 class="title">Parse callbacks</h2>
  <p class="para">
   Parse <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>s are invoked by
   <span class="function"><a href="function.yaml-parse.html" class="function">yaml_parse()</a></span>, <span class="function"><a href="function.yaml-parse-file.html" class="function">yaml_parse_file()</a></span> or
   <span class="function"><a href="function.yaml-parse-url.html" class="function">yaml_parse_url()</a></span> functions when a registered YAML tag is
   encountered. The callback is passed the tagged entity&#039;s value, the tag, and
   flags indicating the scalar entity style. The callback must return the data
   that the YAML parser should emit for this entity.
  </p>
  <div class="example" id="example-3854">
   <p><strong>示例 #1 Parse callback example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Parsing&nbsp;callback&nbsp;for&nbsp;yaml&nbsp;tag.<br />&nbsp;*&nbsp;@param&nbsp;mixed&nbsp;$value&nbsp;Data&nbsp;from&nbsp;yaml&nbsp;file<br />&nbsp;*&nbsp;@param&nbsp;string&nbsp;$tag&nbsp;Tag&nbsp;that&nbsp;triggered&nbsp;callback<br />&nbsp;*&nbsp;@param&nbsp;int&nbsp;$flags&nbsp;Scalar&nbsp;entity&nbsp;style&nbsp;(see&nbsp;YAML_*_SCALAR_STYLE)<br />&nbsp;*&nbsp;@return&nbsp;mixed&nbsp;Value&nbsp;that&nbsp;YAML&nbsp;parser&nbsp;should&nbsp;emit&nbsp;for&nbsp;the&nbsp;given&nbsp;value<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">tag_callback&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tag</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$flags</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">());&nbsp;</span><span style="color: #FF8000">//&nbsp;debugging<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$value</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$yaml&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;YAML<br /></span><span style="color: #DD0000">greeting:&nbsp;!example/hello&nbsp;World<br /></span><span style="color: #007700">YAML;<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">yaml_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$yaml</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ndocs</span><span style="color: #007700">,&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'!example/hello'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'tag_callback'</span><span style="color: #007700">,<br />&nbsp;&nbsp;));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  string(5) &quot;World&quot;
  [1]=&gt;
  string(14) &quot;!example/hello&quot;
  [2]=&gt;
  int(1)
}
array(1) {
  [&quot;greeting&quot;]=&gt;
  string(11) &quot;Hello World&quot;
}
</pre></div>
   </div>
  </div>
 </div></div></div></body></html>